<?php
/**
 * Created by PhpStorm.
 * User: Orange
 * Date: 2018/4/26
 * Time: 10:12
 */

namespace app\admin\controller\user;

use app\common\controller\Backend;

/**
 * 会员列表
 * Class MemberList
 */
class MemberList extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Member');
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->where(array('is_delete' => 0))
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->where(array('is_delete' => 0))
                ->field(['password', 'salt', 'token'], true)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加会员
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->post('row/a');
            if ($data) {
                if (!check_mobile($data['phone'])) {
                    $this->error('请输入正确的手机号');
                }
                $member = model('Member')
                    ->where(array('phone' => $data['phone']))
                    ->where(array('is_delete' => 0))
                    ->find();
                if ($member) {
                    $this->error('手机号已存在');
                }
                model('Member')->create($data);
                $this->success('添加成功');
            }
            $this->error('添加失败');
        }
        return $this->view->fetch();
    }

    /**
     * 修改密码
     * @param string $ids
     * @return string
     */
    public function change_password($ids = '')
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isAjax()) {
            $data = $this->request->post('row/a');
            if (strlen($data['password']) > 12 || strlen($data['password']) < 6) {
                $this->error('密码长度应在6到12位之间');
            }
            if ($data) {
                $row->save($data);
                $this->success('修改成功');
            }
            $this->error('修改失败');
        }
        $this->view->assign('row', $row);
        return $this->view->fetch();
    }

    /**
     * 修改信息
     * @param string $ids
     * @return string
     */
    public function edit($ids = '')
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row)
            $this->error(__('No Results were found'));
        if ($this->request->isAjax()) {
            $data = $this->request->post('row/a');
            if ($data) {
                if (!check_mobile($data['phone'])) {
                    $this->error('请输入正确的手机号');
                }
                $member = model('Member')
                    ->where(array('phone' => $data['phone']))
                    ->where(array('is_delete' => 0))
                    ->find();
                if ($member) {
                    $this->error('手机号已存在');
                }
                $row->save($data);
                $this->success('修改成功');
            }
            $this->error('修改失败');
        }
        $this->view->assign('row', $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            try {
                $this->model->where(array('id' => $ids))->update(array('is_delete' => 1));
                $user_id = $this->model->where(array('id' => $ids))->value('user_id');
                if ($user_id != 0) {
                    model('User')->where(array('id' => $user_id))->update(array('user_type' => 0));
                }
                $this->success('删除成功');
            } catch (Exception $e) {
                $this->error('删除失败');
            }
        }
        $this->error();
    }

    /**
     * 搜索记录
     * @param string $ids
     * @return string|\think\response\Json
     */
    public function access_log($ids = '')
    {
        if ($this->request->isAjax()) {
            $data = model('AccessLog');
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $map['member_id'] = $ids;
            $total = $data
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->count();
            $list = $data
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $nickname = $this->model->where(array('id' => $ids))->value('name');
            foreach ($list as $key => &$value) {
                $value['name'] = $nickname;
            }
            unset($value);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        $this->assignconfig('member_id', $ids);
        return $this->view->fetch();
    }

    /**
     * 登录记录
     * @param string $ids
     * @return string|\think\response\Json
     */
    public function login_log($ids = '')
    {
        if ($this->request->isAjax()) {
            $data = model('LoginLog');
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $map['member_id'] = $ids;
            $total = $data
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->count();
            $list = $data
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $nickname = $this->model->where(array('id' => $ids))->value('name');
            foreach ($list as $key => &$value) {
                $value['name'] = $nickname;
            }
            unset($value);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        $this->assignconfig('member_id', $ids);
        return $this->view->fetch();
    }

    /**
     * 查看详情
     * @param string $ids
     * @return string|\think\response\Json
     */
    public function verify_log($ids = '')
    {
        if ($this->request->isAjax()) {
            $row = model('VerifyLog');
            list($where, $sort, $order) = $this->buildparams();
            $map['member_id'] = $ids;
            $total = $row
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->count();
            $list = $row
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->select();
            foreach ($list as $key => &$value) {
                $bacteriaName = model('Bacteria')->where(array('id' => $value['bacteria_id']))->value('chinese_name');
                $value['bacteria_name'] = $bacteriaName;
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        $this->assignconfig('member_id', $ids);
        return $this->view->fetch();
    }
}